﻿Imports System.IO
Imports System.Drawing.Printing

Public Class 出售记录

#Region "外观处理"

    '关闭窗体
    Private Sub 出售记录_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        LogSearchF = Nothing
    End Sub

    '改变窗体大小
    Private Sub 出售记录_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.Width < 772 Then
            Me.Width = 772
        End If
        If Me.Height < 563 Then
            Me.Height = 563
        End If
    End Sub

#End Region

#Region "控件操作"

    '启用品牌型号条件
    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked Then
            ComboBox1.Enabled = True
            CheckBox2.Enabled = True
            CheckBox2_CheckedChanged(sender, e)
        Else
            ComboBox1.Enabled = False
            CheckBox2.Enabled = False
            ComboBox2.Enabled = False
        End If
        Label8.Visible = False
    End Sub

    '启用型号条件
    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked Then
            ComboBox2.Enabled = True
        Else
            ComboBox2.Enabled = False
        End If
    End Sub

    '启用日期条件
    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged

        If CheckBox4.Checked Then
            TextBox1.Enabled = True
            TextBox2.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = True
            RadioButton1.Enabled = True
            RadioButton2.Enabled = True
            RadioButton1_CheckedChanged(sender, e)
        Else
            TextBox1.Enabled = False
            TextBox2.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
            RadioButton1.Enabled = False
            RadioButton2.Enabled = False
        End If
        Label8.Visible = False
    End Sub

    '显示日期选择器
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MonthCalendar1.Left = 8
        MonthCalendar1.Top = 28
        MonthCalendar1.Visible = True



    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MonthCalendar2.Left = 8
        MonthCalendar2.Top = 28
        MonthCalendar2.Visible = True
    End Sub

    '选择日期
    Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged

        TextBox1.Text = MonthCalendar1.SelectionRange.Start

        MonthCalendar1.Visible = False
    End Sub
    Private Sub MonthCalendar2_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar2.DateChanged

        TextBox2.Text = MonthCalendar2.SelectionRange.Start

        MonthCalendar2.Visible = False

    End Sub

#End Region

    '全局变量
    Dim PrintOutRs As Boolean = True     'ture 为出售 否则为入货
    Dim Page_Count As Integer = 18       '每页记录数
    Dim Page_Mulit As Integer = 0        '打印记录的指针
    Dim Page_Total As Integer = 0        '页数
    Dim Page_NowPr As Integer = 1        '页数指针

    Dim tems As Integer = 0
    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 24)         '标题字体
    Dim ZT_PageC As Font = New Font(New FontFamily("宋体"), 12)         '页脚字体
    Dim ZT_CaleL As Font = New Font(New FontFamily("宋体"), 14)         '汇总字体

    '窗体加载
    Private Sub 出售记录_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        CheckBox3_CheckedChanged(sender, e)
        CheckBox4_CheckedChanged(sender, e)
        Label4.Visible = False
        Label5.Visible = False
        Label6.Visible = False
        Label7.Visible = False
        Label8.Visible = False

    End Sub
    Private Sub 出售记录_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '加载品牌
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            For i = 0 To ds_table.Rows.Count - 1
                ComboBox1.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If
    End Sub

    '加载型号
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try

            If ComboBox1.Text = "" Then
                Exit Sub
            End If

            Dim sqlstr As String = "select 型号,备注 from 型号表 where 所属品牌 = '" + ComboBox1.Text + "'"
            Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
            ComboBox2.Items.Clear()
            Dim ds_table As Data.DataTable = ds.Tables(0)
            If ds_table.Rows.Count > 0 Then
                For i = 0 To ds_table.Rows.Count - 1
                    If ds_table.Rows(i).Item("备注").ToString.Contains("隐藏") And Not CheckBox5.Checked Then

                    Else
                        ComboBox2.Items.Add(ds_table.Rows(i).Item(0).ToString)
                    End If
                Next
            End If

            'ComboBox2.Focus()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus
        ComboBox1_SelectedIndexChanged(sender, e)
    End Sub
    '显示所有型号
    Private Sub CheckBox5_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged
        ComboBox1_SelectedIndexChanged(sender, e)
    End Sub

    '快选今日 
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then
            TextBox1.Text = Now.Year.ToString + "-" + Now.Month.ToString + "-" + Now.Day.ToString
            TextBox2.Text = Now.Year.ToString + "-" + Now.Month.ToString + "-" + Now.Day.ToString
        Else
            Dim aa As Date = Now
            TextBox1.Text = aa.Year.ToString + "-" + aa.Month.ToString + "-" + "1"
            Dim bb As Date = Date.Parse(TextBox1.Text).AddMonths(1).AddDays(-1)
            TextBox2.Text = bb.Year.ToString + "-" + bb.Month.ToString + "-" + bb.Day.ToString
        End If
    End Sub

    '快选本月
    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        RadioButton1_CheckedChanged(sender, e)
    End Sub

    '出售记录按扭
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        '表头
        DataGridView1.Rows.Clear()
        DataGridView1.Columns.Clear()
        DataGridView1.Columns.Add("品牌", "品牌")
        DataGridView1.Columns("品牌").Width = 80
        DataGridView1.Columns.Add("型号", "型号")
        DataGridView1.Columns("型号").Width = 80

        DataGridView1.Columns.Add("入货价", "入货价")
        DataGridView1.Columns("入货价").Width = 60
        DataGridView1.Columns("入货价").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("入货价").Visible = False

        DataGridView1.Columns.Add("售价", "售价")
        DataGridView1.Columns("售价").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("售价").Width = 60

        DataGridView1.Columns.Add("盈利", "盈利")
        DataGridView1.Columns("盈利").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("盈利").Width = 60
        DataGridView1.Columns("盈利").Visible = False

        DataGridView1.Columns.Add("数量", "数量")
        DataGridView1.Columns("数量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView1.Columns("数量").Width = 40
        DataGridView1.Columns.Add("日期", "日期")
        DataGridView1.Columns("日期").Width = 150
        DataGridView1.Columns.Add("客户", "客户")
        DataGridView1.Columns("客户").Width = 60
        DataGridView1.Columns.Add("联系方式", "联系方式")
        DataGridView1.Columns("联系方式").Width = 100
        DataGridView1.Columns.Add("地址", "地址")
        DataGridView1.Columns("地址").Width = 250

        CheckBox1.Enabled = True
        PrintOutRs = True

        '搜索出售表

        Dim sqlstr As String = "select * from 出售表"

        If CheckBox3.Checked Or CheckBox4.Checked Then
            sqlstr += " where "
            '品牌型号过滤
            If CheckBox3.Checked Then
                '空输入处理
                If Trim(ComboBox1.Text) = "" Then MsgBox("请选择商品的品牌！") : ComboBox1.Focus() : Exit Sub

                sqlstr += "品牌 ='" + ComboBox1.Text + "'"

                If CheckBox2.Checked Then
                    If Trim(ComboBox2.Text) = "" Then MsgBox("请选择商品的型号！") : ComboBox2.Focus() : Exit Sub
                    sqlstr += " and 型号 ='" + ComboBox2.Text + "'"
                End If
            End If '此部分完成

            '日期过滤
            If CheckBox4.Checked Then
                If Trim(TextBox1.Text) = "" Then MsgBox("请选择起始日期") : TextBox1.Focus() : Exit Sub
                If Trim(TextBox2.Text) = "" Then MsgBox("请选择终止日期") : TextBox2.Focus() : Exit Sub
                If CheckBox3.Checked Then sqlstr += " and "
                Dim d1 As Date = Date.Parse(TextBox2.Text)
                Dim d2 As Date = d1.AddDays(1)
                sqlstr += "日期 >= #" + TextBox1.Text + "# and 日期 < #" + d2 + "#"
            End If
        End If

        Debug.Print(sqlstr)

        Dim ds As DataTable = SQLtoDataSet(sqlstr).Tables(0)
        Debug.Print(ds.Rows.Count.ToString)
        If ds.Rows.Count > 0 Then
            For i = 0 To ds.Rows.Count - 1
                Dim rs As DataRow = ds.Rows(i)
                '获取入货价
                Dim sqltem As String = "select 入货单价 from 入货表 where 品牌 = '" + rs.Item("品牌").ToString + "' and 型号 = '" + rs.Item("型号").ToString + "'"
                Dim dssst As DataTable = SQLtoDataSet(sqltem).Tables(0)
                Dim INprice As String = "不明"
                Dim RL As String = "不明"
                If dssst.Rows.Count > 0 Then

                    INprice = dssst.Rows(0).Item(0).ToString
                    RL = ((rs.Item("售出单价") - Val(INprice)) * rs.Item("售出数量")).ToString

                End If

                Dim row1() As String = {rs.Item("品牌").ToString, rs.Item("型号").ToString, INprice, rs.Item("售出单价").ToString, RL, rs.Item("售出数量").ToString, rs.Item("日期").ToString, rs.Item("购机客户").ToString, rs.Item("联系电话").ToString, rs.Item("地址").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If  '此部分完成

        Dim SL As Integer = 0
        Dim SR As Integer = 0
        Dim CB As Integer = 0
        Dim RL1 As Integer = 0
        For j = 0 To DataGridView1.Rows.Count - 1
            SL += DataGridView1.Rows(j).Cells("数量").Value
            SR += DataGridView1.Rows(j).Cells("售价").Value * DataGridView1.Rows(j).Cells("数量").Value
            CB += DataGridView1.Rows(j).Cells("入货价").Value * DataGridView1.Rows(j).Cells("数量").Value
            RL1 += DataGridView1.Rows(j).Cells("盈利").Value
        Next
        Label4.Text = "总出售数量：" + SL.ToString + "台" : Label4.Visible = True
        Label5.Text = "总收入金额：" + SR.ToString + "元" : Label5.Visible = True
        Label6.Text = "成本总消耗：" + CB.ToString + "元"
        Label7.Text = "总盈利金额：" + RL1.ToString + "元"

    End Sub

    '入货记录按扭
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        CheckBox1.Checked = False
        CheckBox1.Enabled = False
        PrintOutRs = False

        '表头
        DataGridView1.Rows.Clear()
        DataGridView1.Columns.Clear()
        DataGridView1.Columns.Add("品牌", "品牌")
        DataGridView1.Columns.Add("型号", "型号")
        DataGridView1.Columns.Add("入货价", "入货价")
        DataGridView1.Columns.Add("数量", "数量")
        DataGridView1.Columns.Add("日期", "日期")
        DataGridView1.Columns.Add("备注", "备注")


        DataGridView1.Columns("品牌").Width = 80
        DataGridView1.Columns("型号").Width = 80
        DataGridView1.Columns("入货价").Width = 60
        DataGridView1.Columns("入货价").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("数量").Width = 40
        DataGridView1.Columns("数量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView1.Columns("日期").Width = 140
        DataGridView1.Columns("备注").Width = 140

        '搜索入货表

        Dim sqlstr1 As String = "select * from 入货表"

        If CheckBox3.Checked Or CheckBox4.Checked Then
            sqlstr1 += " where "
            '品牌型号过滤
            If CheckBox3.Checked Then
                '空输入处理
                If Trim(ComboBox1.Text) = "" Then MsgBox("请选择商品的品牌！") : ComboBox1.Focus() : Exit Sub

                sqlstr1 += "品牌 ='" + ComboBox1.Text + "'"

                If CheckBox2.Checked Then
                    If Trim(ComboBox2.Text) = "" Then MsgBox("请选择商品的型号！") : ComboBox2.Focus() : Exit Sub
                    sqlstr1 += " and 型号 ='" + ComboBox2.Text + "'"
                End If
            End If

            '日期过滤
            If CheckBox4.Checked Then
                If Trim(TextBox1.Text) = "" Then MsgBox("请选择起始日期") : TextBox1.Focus() : Exit Sub
                If Trim(TextBox2.Text) = "" Then MsgBox("请选择终止日期") : TextBox2.Focus() : Exit Sub
                If CheckBox3.Checked Then sqlstr1 += " and "
                Dim d1 As Date = Date.Parse(TextBox2.Text)
                Dim d2 As Date = d1.AddDays(1)
                sqlstr1 += "日期 >= #" + TextBox1.Text + "# and 日期 < #" + d2.ToString + "#"
            End If
        End If
        Debug.Print(sqlstr1)
        Dim ds1 As DataTable = SQLtoDataSet(sqlstr1).Tables(0)
        Debug.Print(ds1.Rows.Count.ToString)
        If ds1.Rows.Count > 0 Then
            For i = 0 To ds1.Rows.Count - 1
                Dim rs As DataRow = ds1.Rows(i)
                Dim row1() As String = {rs.Item("品牌").ToString, rs.Item("型号").ToString, rs.Item("入货单价").ToString, rs.Item("数量").ToString, rs.Item("日期").ToString, rs.Item("备注1").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If

        Dim SL As Integer = 0
        Dim CB As Integer = 0

        For j = 0 To DataGridView1.Rows.Count - 1
            SL += DataGridView1.Rows(j).Cells("数量").Value
            CB += DataGridView1.Rows(j).Cells("入货价").Value * DataGridView1.Rows(j).Cells("数量").Value
        Next
        Label4.Text = "总入货数量：" + SL.ToString + "台"
        Label5.Text = "成本总消耗：" + CB.ToString + "元"
        Label4.Visible = True
        Label5.Visible = True
        Label6.Visible = False
        Label7.Visible = False

    End Sub

    '显示盈利选择
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            DataGridView1.Columns("盈利").Visible = True
            DataGridView1.Columns("入货价").Visible = True
            Label6.Visible = True
            Label7.Visible = True
        Else
            DataGridView1.Columns("盈利").Visible = False
            DataGridView1.Columns("入货价").Visible = False
            Label6.Visible = False
            Label7.Visible = False
        End If
    End Sub



    '打印按扭
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        If DataGridView1.Rows.Count = 0 Then
            MsgBox("没有记录可进行打印操作")
            Exit Sub
        End If


        '强制横向打印
        PageSetupDialog1.PageSettings.Landscape = True

        '预览
        If PageSetupDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
            PrintPreviewDialog1.ShowDialog()
        End If



    End Sub

    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        '打算页数
        Dim PageCtem As Integer = DataGridView1.Rows.Count
        If PageCtem > Page_Count Then
            '需分页
            Page_Total = PageCtem \ Page_Count + 1
        Else
            '不需分页
            Page_Total = 1
        End If

        '执行打印
        If PrintOutRs Then
            '打印出售记录
            PrintOrs(e)
        Else
            '打印入货记录
            PrintIrs(e)
        End If

    End Sub
    '打印出售记录
    Private Sub PrintOrs(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '打印表头及汇总线
        PrintTop(g, rect)

        '打印内容
        Dim y = 170
        For i = (Page_NowPr - 1) * Page_Count To Page_NowPr * Page_Count - 1
            Dim xx As Integer = rect.Left + 22
            Dim dr As DataGridViewRow = DataGridView1.Rows(i)
            g.DrawString(dr.Cells.Item("品牌").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 70
            g.DrawString(dr.Cells.Item("型号").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 70

            If CheckBox1.Checked Then
                g.DrawString(dr.Cells.Item("入货价").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 60
            End If

            g.DrawString(dr.Cells.Item("售价").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 60

            If CheckBox1.Checked Then
                g.DrawString(dr.Cells.Item("盈利").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 60
            End If

            g.DrawString(dr.Cells.Item("数量").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 50

            Dim da As Date = Date.Parse(dr.Cells.Item("日期").Value.ToString)
            Dim RQ As String = da.Year.ToString + "-" + da.Month.ToString + "-" + da.Day.ToString
            g.DrawString(RQ, ZT_PageC, Brushes.Black, xx, y) : xx += 120

            g.DrawString(dr.Cells.Item("客户").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 120
            g.DrawString(dr.Cells.Item("联系方式").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 120
            g.DrawString(dr.Cells.Item("地址").Value.ToString, ZT_PageC, Brushes.Black, xx, y)
            y += 30
            If i = DataGridView1.Rows.Count - 1 Then Exit For
        Next

        '判断页数
        Dim PageCtem As Integer = DataGridView1.Rows.Count

        If Page_NowPr = Page_Total Then
            Page_NowPr = 1
            Page_Total = 0
            Exit Sub
        End If

        If PageCtem > Page_Count Then
            '需分页
            Page_NowPr += 1
            e.HasMorePages = True
        End If


    End Sub
    '打印入货记录
    Private Sub PrintIrs(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '记录打印内容

        '打印表头及汇总线
        PrintTop(g, rect)

        '打印内容
        Dim y = 170
        For i = (Page_NowPr - 1) * Page_Count To Page_NowPr * Page_Count - 1
            Dim xx As Integer = rect.Left + 22
            Dim dr As DataGridViewRow = DataGridView1.Rows(i)
            g.DrawString(dr.Cells.Item("品牌").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 130
            g.DrawString(dr.Cells.Item("型号").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 130
            g.DrawString(dr.Cells.Item("入货价").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 130
            g.DrawString(dr.Cells.Item("数量").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 130
            g.DrawString(dr.Cells.Item("日期").Value.ToString, ZT_PageC, Brushes.Black, xx, y) : xx += 200
            g.DrawString(dr.Cells.Item("备注").Value.ToString, ZT_PageC, Brushes.Black, xx, y)
            y += 30
            If i = DataGridView1.Rows.Count - 1 Then Exit For
        Next

        '判断页数
        Dim PageCtem As Integer = DataGridView1.Rows.Count

        If Page_NowPr = Page_Total Then
            Page_NowPr = 1
            Page_Total = 0
            Exit Sub
        End If

        If PageCtem > Page_Count Then
            '需分页
            Page_NowPr += 1
            e.HasMorePages = True
        End If

    End Sub

    '打印表头、汇总线、及汇总
    Private Sub PrintTop(ByVal g As Graphics, ByVal rect As Rectangle)

        '汇总线
        g.DrawLine(Pens.Black, rect.Left + 20, 120, rect.Right - 20, 120)

        '打印时间及页码
        g.DrawString("Page " + Page_NowPr.ToString + "/" + Page_Total.ToString + "  打印时间：" + Now.ToString, ZT_PageC, Brushes.Black, 830, 770)

        If PrintOutRs Then
            '打印出售记录时

            '标题
            g.DrawString("绍康电器商店出售记录报表", ZT_title, Brushes.Black, 350, 30)

            '首页打印汇总
            If Page_NowPr = 1 Then

                Dim SL As Integer = 0
                Dim SR As Integer = 0
                Dim CB As Integer = 0
                Dim RL As Integer = 0
                For j = 0 To DataGridView1.Rows.Count - 1
                    SL += DataGridView1.Rows(j).Cells("数量").Value
                    SR += DataGridView1.Rows(j).Cells("售价").Value * DataGridView1.Rows(j).Cells("数量").Value
                    CB += DataGridView1.Rows(j).Cells("入货价").Value * DataGridView1.Rows(j).Cells("数量").Value
                    RL += DataGridView1.Rows(j).Cells("盈利").Value
                Next

                Dim xtem As Integer = rect.Left + 20
                g.DrawString("出售数量：" + SL.ToString + "台", ZT_CaleL, Brushes.Black, xtem, 95)
                g.DrawString("收入总额：" + SR.ToString + "元", ZT_CaleL, Brushes.Black, xtem + 200, 95)
                If CheckBox1.Checked Then
                    g.DrawString("成本总耗：" + CB.ToString + "元", ZT_CaleL, Brushes.Black, xtem + 500, 95)
                    g.DrawString("盈利总额：" + RL.ToString + "元", ZT_CaleL, Brushes.Black, xtem + 800, 95)
                End If
            End If

            '表头
            Dim xx As Integer = rect.Left + 22
            g.DrawString("品牌", ZT_PageC, Brushes.Black, xx, 135) : xx += 70
            g.DrawString("型号", ZT_PageC, Brushes.Black, xx, 135) : xx += 70

            If CheckBox1.Checked Then
                g.DrawString("入货价", ZT_PageC, Brushes.Black, xx, 135) : xx += 60
            End If


            g.DrawString("售价", ZT_PageC, Brushes.Black, xx, 135) : xx += 60


            If CheckBox1.Checked Then
                g.DrawString("盈利", ZT_PageC, Brushes.Black, xx, 135) : xx += 60
            End If

            g.DrawString("数量", ZT_PageC, Brushes.Black, xx, 135) : xx += 50
            g.DrawString("日期", ZT_PageC, Brushes.Black, xx, 135) : xx += 120
            g.DrawString("客户", ZT_PageC, Brushes.Black, xx, 135) : xx += 120
            g.DrawString("联系方式", ZT_PageC, Brushes.Black, xx, 135) : xx += 120
            g.DrawString("地址", ZT_PageC, Brushes.Black, xx, 135)

        Else
            '打印入货记录时

            '标题
            g.DrawString("绍康电器商店入货记录报表", ZT_title, Brushes.Black, 350, 30)

            '首页打印汇总
            If Page_NowPr = 1 Then

                Dim SL As Integer = 0
                Dim CB As Integer = 0

                For j = 0 To DataGridView1.Rows.Count - 1
                    SL += DataGridView1.Rows(j).Cells("数量").Value
                    CB += DataGridView1.Rows(j).Cells("入货价").Value * DataGridView1.Rows(j).Cells("数量").Value
                Next

                Dim xtem As Integer = rect.Left + 20
                g.DrawString("入货数量：" + SL.ToString + "台", ZT_CaleL, Brushes.Black, xtem, 95)
                g.DrawString("成本总耗：" + CB.ToString + "元", ZT_CaleL, Brushes.Black, xtem + 400, 95)
            End If

            '表头
            Dim xx As Integer = rect.Left + 22
            g.DrawString("品  牌", ZT_PageC, Brushes.Black, xx, 135) : xx += 130
            g.DrawString("型  号", ZT_PageC, Brushes.Black, xx, 135) : xx += 130
            g.DrawString("入货价", ZT_PageC, Brushes.Black, xx, 135) : xx += 130
            g.DrawString("数  量", ZT_PageC, Brushes.Black, xx, 135) : xx += 130
            g.DrawString("日  期", ZT_PageC, Brushes.Black, xx, 135) : xx += 200
            g.DrawString("备  注", ZT_PageC, Brushes.Black, xx, 135)

        End If
    End Sub

    '电话检索按扭
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Sstr As String = InputBox("请输入手机号码", "输入手机号码")

        If Sstr.Trim = "" Then
            Exit Sub
        Else
            CheckBox1.Checked = False
            CheckBox2.Checked = False
            CheckBox3.Checked = False
            CheckBox4.Checked = False

            Label8.Text = "目前正显示手机号：" + Sstr.ToString.Trim + " 的订单"
            Label8.Visible = True
            LoadByPhone(Sstr.ToString.Trim)

        End If

    End Sub
    Private Sub LoadByPhone(ph)

        '表头
        DataGridView1.Rows.Clear()
        DataGridView1.Columns.Clear()
        DataGridView1.Columns.Add("品牌", "品牌")
        DataGridView1.Columns("品牌").Width = 80
        DataGridView1.Columns.Add("型号", "型号")
        DataGridView1.Columns("型号").Width = 80

        DataGridView1.Columns.Add("入货价", "入货价")
        DataGridView1.Columns("入货价").Width = 60
        DataGridView1.Columns("入货价").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("入货价").Visible = False

        DataGridView1.Columns.Add("售价", "售价")
        DataGridView1.Columns("售价").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("售价").Width = 60

        DataGridView1.Columns.Add("盈利", "盈利")
        DataGridView1.Columns("盈利").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns("盈利").Width = 60
        DataGridView1.Columns("盈利").Visible = False

        DataGridView1.Columns.Add("数量", "数量")
        DataGridView1.Columns("数量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView1.Columns("数量").Width = 40
        DataGridView1.Columns.Add("日期", "日期")
        DataGridView1.Columns("日期").Width = 150
        DataGridView1.Columns.Add("客户", "客户")
        DataGridView1.Columns("客户").Width = 60
        DataGridView1.Columns.Add("联系方式", "联系方式")
        DataGridView1.Columns("联系方式").Width = 100
        DataGridView1.Columns.Add("地址", "地址")
        DataGridView1.Columns("地址").Width = 250

        CheckBox1.Enabled = True
        PrintOutRs = True

        '搜索出售表

        Dim sqlstr As String = "select * from 出售表 where 联系电话 like'%" + ph + "%' or 地址 like'%" + ph + "%'"

        Debug.Print(sqlstr)

        Dim ds As DataTable = SQLtoDataSet(sqlstr).Tables(0)
        Debug.Print(ds.Rows.Count.ToString)
        If ds.Rows.Count > 0 Then
            For i = 0 To ds.Rows.Count - 1
                Dim rs As DataRow = ds.Rows(i)
                '获取入货价
                Dim sqltem As String = "select 入货单价 from 入货表 where 品牌 = '" + rs.Item("品牌").ToString + "' and 型号 = '" + rs.Item("型号").ToString + "'"
                Dim dssst As DataTable = SQLtoDataSet(sqltem).Tables(0)
                Dim INprice As String = "不明"
                Dim RL As String = "不明"
                If dssst.Rows.Count > 0 Then

                    INprice = dssst.Rows(0).Item(0).ToString
                    RL = ((rs.Item("售出单价") - Val(INprice)) * rs.Item("售出数量")).ToString

                End If

                Dim row1() As String = {rs.Item("品牌").ToString, rs.Item("型号").ToString, INprice, rs.Item("售出单价").ToString, RL, rs.Item("售出数量").ToString, rs.Item("日期").ToString, rs.Item("购机客户").ToString, rs.Item("联系电话").ToString, rs.Item("地址").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If  '此部分完成

        Dim SL As Integer = 0
        Dim SR As Integer = 0
        Dim CB As Integer = 0
        Dim RL1 As Integer = 0
        For j = 0 To DataGridView1.Rows.Count - 1
            SL += DataGridView1.Rows(j).Cells("数量").Value
            SR += DataGridView1.Rows(j).Cells("售价").Value * DataGridView1.Rows(j).Cells("数量").Value
            CB += DataGridView1.Rows(j).Cells("入货价").Value * DataGridView1.Rows(j).Cells("数量").Value
            RL1 += DataGridView1.Rows(j).Cells("盈利").Value
        Next
        Label4.Text = "总出售数量：" + SL.ToString + "台" : Label4.Visible = True
        Label5.Text = "总收入金额：" + SR.ToString + "元" : Label5.Visible = True
        Label6.Text = "成本总消耗：" + CB.ToString + "元"
        Label7.Text = "总盈利金额：" + RL1.ToString + "元"

    End Sub


End Class